<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    private $_id;

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        $record = ADMINLOGIN::model()->findByAttributes(array('user' => $this->username));
        if (is_null($record)) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($record->password != $this->password) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            $this->_id = base64_encode($record->id_admin);

            $record2 = HAKAKSES::model()->findByAttributes(array('id_hakAkses' => $record->id_hakAkses));

            switch ($record->id_hakAkses) {
                case 1: $role = 'Super Admin';
                    break;
                case 2: $role = 'Admin TK Rawamangun';
                    break;
                case 3: $role = 'Admin SMP Rawamangun';
                    break;
                case 4: $role = 'Admin SMA Rawamangun';
                    break;
                case 5: $role = 'Admin SMP Cibubur';
                    break;
                case 6: $role = 'Admin SMA Cibubur';
                    break;
                case 7: $role = 'Admin SMP Kebayoran';
                    break;
                case 8: $role = 'Admin SMA Kebayoran';
                    break;
                default:
                    $role = '';
            }

            switch ($record2->id_adminGroup) {
                case 1: $roleGroup = 'Admin';
                    break;
                case 2: $roleGroup = 'Admin TK';
                    break;
                case 3: $roleGroup = 'Admin SMP';
                    break;
                case 4: $roleGroup = 'Admin SMA';
                    break;
                default:
                    $role = '';
            }

            //SCHOOL
            $school = base64_encode($record->id_hakAkses);
            $role=base64_encode($role);
            $roleGroup=base64_encode($roleGroup);
            $this->setState('role', $role);
            $this->setState('roleGroup', $roleGroup);
            $this->setState('school', $school);
            $this->setState('id',$this->getId());

            //$this->setState('nome', $record->nome);
            $this->errorCode = self::ERROR_NONE;
        }
        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}